package com.mjh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjh.mapper.AnnouncementMapper;
import com.mjh.pojo.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 *
 * 公告 前端控制器
 *
 *
 * @author MJH
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 公告查询
     * @param isEmphasis  是否为重要公告   1 重要
     * @param isNew  是否为最新公告  true 最新公告
     * @param currentPage  当前页
     * @param pageSize  每页数量
     * @return
     */
    @GetMapping("/query")
    public ResponseEntity<Object>  queryAnnoun(Integer isEmphasis, Boolean isNew,
                                               @RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "10") Integer pageSize){

        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.select("f_ggtp","title","absrtact","id","create_time");
        if(isEmphasis!=null && isEmphasis==1){
            wrapper.ge("f_Sfwzygg",0);
        }
        if (isNew!=null && isNew){
            wrapper.orderByDesc("create_time");
        }

        Page<Announcement> page=null;

        if(currentPage!=null && pageSize!=null){
           //避免传入负数
            page=new Page<>(Math.abs(currentPage),Math.abs(pageSize));
        }
        if (page!=null){
            Page<Announcement> announcementPage = announcementMapper.selectPage(page, wrapper);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total",announcementPage.getTotal());
            map.put("list",announcementPage.getRecords());
            map.put("current_page",announcementPage.getCurrent());

            return ResponseEntity.ok(map);

        }

        List<Announcement> announcementList = announcementMapper.selectList(wrapper);

        return ResponseEntity.ok(announcementList);
    }


    /**
     * 根据Id查询
     * @param annId
     * @return
     */
    @GetMapping("/querybyId/{annId}")
    public ResponseEntity<Object> queryAnnByID(@PathVariable("annId") Integer annId){
        Announcement announcement = announcementMapper.selectById(annId);
        return ResponseEntity.ok(announcement);
    }



}
